//
//  AddressTableView.m
//  AddressTest
//
//  Created by weizhongming on 2017/2/15.
//
#define RGB(r, g, b)  [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:1]

#import "AddressTableView.h"
#import "CityVO.h"
#import "CitiesVO.h"
#import "AddressTableViewCell.h"

@implementation AddressTableView

- (instancetype)initWithFrame:(CGRect)frame withParmas:(id)params{

    self = [super initWithFrame:frame];
    
    if (self) {
        
        self.datas = params;
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [[UIView alloc] init];
        [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];

        [self addSubview:_tableView];
    }
    
    return self;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    for (int i = 0; i < self.datas.count; i++) {
        if (section == i) {
            if (![self.datas[i] isKindOfClass:[NSString class]]) {
                return [self.datas[i] count];
            }
            
        }
    }
    return 0;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.datas.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 50;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *cellName = @"cellid";
    AddressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName];
    if (!cell)
    {
        cell = [[AddressTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellName];
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    
    for (int i = 0; i < self.datas.count; i++) {
        if (indexPath.section == i) {
            if ([self.datas[i][indexPath.row] isKindOfClass:[CityVO class]]) {
                
                CityVO *cityVO = self.datas[i][indexPath.row];
                cell.cityLab.text = cityVO.state;
                
                //            if (indexPath.row == 0) {
                cell.prefixLab.text = [AddressTableView getFirstLetterFromString:cityVO.state];
                //            }
            }
            
              if ([self.datas[i][indexPath.row] isKindOfClass:[CitiesVO class]]){
                
                CitiesVO *citiesVO = self.datas[i][indexPath.row];
                cell.cityLab.text = citiesVO.city;
                  
                  //            if (indexPath.row == 0) {
                  cell.prefixLab.text = [AddressTableView getFirstLetterFromString:citiesVO.city];
                  //            }
            }else if ([self.datas[indexPath.row] isKindOfClass:[NSString class]]){
                
                 cell.textLabel.text = self.datas[i][indexPath.row];
            }
        }
    }

    
    if (indexPath.row != 0) {
        cell.prefixLab.text = @"";
    }
 
    
    if (self.indexPath && self.indexPath == indexPath) {
        
        cell.imv.image = [UIImage imageNamed:@"icon_choose_purple"];
    }else{
        
        cell.imv.image = [UIImage imageNamed:@""];
    }
    
    return cell;
}

#pragma mark - 获取联系人姓名首字母(传入汉字字符串, 返回大写拼音首字母)
+ (NSString *)getFirstLetterFromString:(NSString *)aString
{
    /**
     * **************************************** START ***************************************
     * 之前PPGetAddressBook对联系人排序时在中文转拼音这一部分非常耗时
     * 参考博主-庞海礁先生的一文:iOS开发中如何更快的实现汉字转拼音 http://www.olinone.com/?p=131
     * 使PPGetAddressBook对联系人排序的性能提升 3~6倍, 非常感谢!
     */
    NSMutableString *mutableString = [NSMutableString stringWithString:aString];
    CFStringTransform((CFMutableStringRef)mutableString, NULL, kCFStringTransformToLatin, false);
    NSString *pinyinString = [mutableString stringByFoldingWithOptions:NSDiacriticInsensitiveSearch locale:[NSLocale currentLocale]];
    /**
     *  *************************************** END ******************************************
     */
    
    // 将拼音首字母装换成大写
    NSString *strPinYin = [[self polyphoneStringHandle:aString pinyinString:pinyinString] uppercaseString];
    // 截取大写首字母
    NSString *firstString = [strPinYin substringToIndex:1];
    // 判断姓名首位是否为大写字母
    NSString * regexA = @"^[A-Z]$";
    NSPredicate *predA = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regexA];
    // 获取并返回首字母
    return [predA evaluateWithObject:firstString] ? firstString : @"#";
    
}

/**
 多音字处理
 */
+ (NSString *)polyphoneStringHandle:(NSString *)aString pinyinString:(NSString *)pinyinString
{
    if ([aString hasPrefix:@"长"]) { return @"chang";}
    if ([aString hasPrefix:@"沈"]) { return @"shen"; }
    if ([aString hasPrefix:@"厦"]) { return @"xia";  }
    if ([aString hasPrefix:@"地"]) { return @"di";   }
    if ([aString hasPrefix:@"重"]) { return @"chong";}
    return pinyinString;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    
    self.indexPath = indexPath;
    
    
    [tableView reloadData];
    
    if (self.block) {
            
        self.block(indexPath);
    }
    
}




@end
